
#include <stdlib.h>

#include <cedule/frame.h>

#include "client.h"
#include "protocol.h"

int client_run( Client_t * client ) {
	Protocol_t * protocol;
	Frame_t * frame;

	frame = frame_create();
	if ( ! frame ) goto error;

	// read frame
	frame_recv( frame, client->fd );
	
	protocol = client->protocol;
	protocol_parse( protocol, frame );
	// reset idletime
	client->idle = 0;

	return 0;

error:
	return -1;
}

Client_t * client_create( int fd ) {
	Client_t * client = NULL;

	client = (Client_t *) malloc (sizeof( Client_t ) );
	if ( !client ) goto error;

	client->fd = fd;
	client->idle = 0;
	client->next = NULL;
	client->protocol = protocol_create();
	if ( ! client->protocol )
		goto error;

	return client;

error:
	if (client ) free( client );
	return NULL;
}

void client_destroy( Client_t * client ) {
	free( client );
}
